import { t } from 'i18next'
export interface SimpleMtEntity {
	mt_id: string
	mt_key: string
	mt_ip?: string
	/**帐号 */
	account: string
	/**帐号类型  "1:moid", "4:非系统邮箱", "5:e164号", "6:电话", "7:ip地址", "8:别名@ip(监控前端)"   2:会议室*/
	account_type: 1 | 4 | 5 | 6 | 7 | 8 | 2
	/**所属会议e164号, 级联会议时为本级会议e164号 */
	conf_e164?: string
	/**名称 */
	name: string
	/**终端呼叫码率 */
	bitrate: number
	/**呼叫协议 "0:H323", "1:SIP", "2:RTC", "255:AUTO" */
	protocol?: 0 | 1 | 2 | 255
	/**是否常用终端 "0:否", "1:是"*/
	often_mt?: 0 | 1
	/**是否互动终端 "0:否", "1:是"*/
	interactive_mt?: 0 | 1
	/**终端类型 "1:普通终端", "3:电话终端" .等...*/
	mt_type: number
	/**e164*/
	e164: string
	/**头像*/
	portrait: string
	/**是否在线 */
	online: 0 | 1
	/**是否多流 */
	stream: 0 | 1
}
export interface MtEntity extends SimpleMtEntity {
	moid: string
	/**级联树ID */
	cascade_id: string
	/**呼叫方式 0-手动;2-定时呼叫;3-追呼;*/
	call_mode: 0 | 2 | 3
	/**终端呼叫类型 1-IP;2-E164;3-323别名;6-电话;9-SIP别名;*/
	call_type: 1 | 2 | 3 | 6 | 9
	/**账号密级 0-未定义;1-公开;2-内部;3-秘密;4-机密;*/
	security_level: 0 | 1 | 2 | 3 | 4
	/**是否外设终端 */
	is_seqp_mt: 0 | 1
	/**终端级联级别 1-上级;2-本级;3-下级;*/
	conf_level: 1 | 2 | 3
	/**终端顺序*/
	index: number
	/**0-智能混音; 1-定制混音; */
	mix?: 0 | 1
	/**是否vip */
	vip?: 0 | 1
	/**是否发送双流 */
	doubleflow?: 0 | 1
	/**是否静音 */
	silence?: 0 | 1
	/**是否开启电视墙 */
	tvwall?: 0 | 1
	/**是否开启画面合成 */
	vmp?: 0 | 1
	/**是否开启轮询 */
	poll?: 0 | 1
	/**是否开启强制广播 */
	force_brd?: 0 | 1
	/**是否开启选看 */
	select?: 0 | 1
	/**是否录像 */
	rec?: 0 | 1
	/**是否上传终端 */
	up_mt?: 0 | 1
	/**是否哑音 */
	mute?: 0 | 1
	/**是否有协作权限 */
	enable_dcs?: 0 | 1
	/**是否在数据会议协助列表中 */
	dcs_oper?: 0 | 1
	/**是否终端自主多画面 */
	mt_vmp?: 0 | 1
	/**是否主讲 */
	speaker?: 0 | 1
	/**是否主席 */
	chairman?: 0 | 1
	/**是否无视频源 */
	video_src?: 0 | 1
}
export type MtStatus = ReturnType<typeof transStatus>
function transStatus(mt: MtEntity) {
	return {
		isOnline: !!mt.online,
		isSilence: !!mt.silence,
		isMute: !!mt.mute,
		isMix: !!mt.mix,
		isVmp: !!mt.vmp,
		isPoll: !!mt.poll,
		isSelect: !!mt.select,
		isTvWall: !!mt.tvwall,
		isDoubleFlow: !!mt.doubleflow,
		isStream: !!mt.stream,
		isUpMt: !!mt.up_mt,
		isVip: !!mt.vip,
		isOftenMt: !!mt.often_mt,
		isForceBrd: !!mt.force_brd,
		isInteractive: !!mt.interactive_mt,
		enableDcs: !!mt.enable_dcs,
		isDcs: !!mt.dcs_oper,
		isSpeaker: !!mt.speaker,
		isChairman: !!mt.chairman,
		isRec: !!mt.rec,
		isMtVmp: !!mt.mt_vmp,
		isSeqpMt: !!mt.is_seqp_mt,
		isVideoSrc: !!mt.video_src,
		get isManager() {
			return this.isChairman || this.isSpeaker
		},
	}
}
export class Mt {
	INITIAL_NAME: string
	isMt: true = true
	moid: string
	account: string
	accountType: 1 | 4 | 5 | 6 | 7 | 8 | 2
	mtid: string
	mtKey: string
	e164: string
	confE164: string
	mtType: number
	cascadeId: string
	confLevel: 1 | 2 | 3
	index: number
	portrait: string
	status: MtStatus
	mtIp?: string
	securityLevel: 0 | 1 | 2 | 3 | 4
	protocol?: 0 | 1 | 2 | 255
	callType: 1 | 2 | 3 | 6 | 9
	callMode: 0 | 2 | 3
	bitrate: number
	constructor(mt: MtEntity) {
		this.INITIAL_NAME = mt.name?.replace(/&nbsp;/g, ' ') ?? ''
		this.moid = mt.moid
		this.account = mt.account
		this.accountType = mt.account_type
		this.mtid = mt.mt_id
		this.mtKey = mt.mt_key
		this.e164 = mt.e164 || ''
		this.confE164 = mt.conf_e164 || ''
		this.mtType = mt.mt_type
		this.cascadeId = mt.cascade_id
		this.confLevel = mt.conf_level
		this.index = mt.index
		this.portrait = mt.portrait
		this.status = transStatus(mt)
		this.mtIp = mt.mt_ip
		this.securityLevel = mt.security_level
		this.protocol = mt.protocol
		this.callType = mt.call_type
		this.callMode = mt.call_mode
		this.bitrate = mt.bitrate
	}
	get name(): string {
		if (this.status.isStream && this.INITIAL_NAME.indexOf(t('[多流]')) === -1 && this.status.isOnline) {
			return t('[多流]') + this.INITIAL_NAME
		}
		return this.INITIAL_NAME
	}
	get isSimpleCascade(): boolean {
		return this.mtType === 9
	}
	get isPhone(): boolean {
		return this.mtType === 3
	}
}
export function revert_mt(mt: Mt): MtEntity & { isMt: true } {
	return {
		isMt: true,
		moid: mt.moid,
		account: mt.account,
		account_type: mt.accountType,
		name: mt.INITIAL_NAME,
		mt_id: mt.mtid,
		mt_key: mt.mtKey,
		e164: mt.e164,
		conf_e164: mt.confE164,
		mt_type: mt.mtType,
		cascade_id: mt.cascadeId,
		conf_level: mt.confLevel,
		index: mt.index,
		portrait: mt.portrait,
		online: mt.status.isOnline ? 1 : 0,
		silence: mt.status.isSilence ? 1 : 0,
		mute: mt.status.isMute ? 1 : 0,
		mix: mt.status.isMix ? 1 : 0,
		vmp: mt.status.isVmp ? 1 : 0,
		poll: mt.status.isPoll ? 1 : 0,
		select: mt.status.isSelect ? 1 : 0,
		tvwall: mt.status.isTvWall ? 1 : 0,
		doubleflow: mt.status.isDoubleFlow ? 1 : 0,
		stream: mt.status.isStream ? 1 : 0,
		up_mt: mt.status.isUpMt ? 1 : 0,
		vip: mt.status.isVip ? 1 : 0,
		often_mt: mt.status.isOftenMt ? 1 : 0,
		force_brd: mt.status.isForceBrd ? 1 : 0,
		interactive_mt: mt.status.isInteractive ? 1 : 0,
		enable_dcs: mt.status.enableDcs ? 1 : 0,
		dcs_oper: mt.status.isDcs ? 1 : 0,
		speaker: mt.status.isSpeaker ? 1 : 0,
		chairman: mt.status.isChairman ? 1 : 0,
		rec: mt.status.isRec ? 1 : 0,
		mt_vmp: mt.status.isMtVmp ? 1 : 0,
		call_mode: mt.callMode,
		call_type: mt.callType,
		security_level: mt.securityLevel,
		is_seqp_mt: mt.status.isSeqpMt ? 1 : 0,
		bitrate: mt.bitrate,
		mt_ip: mt.mtIp,
		protocol: mt.protocol,
		video_src: mt.status.isVideoSrc ? 1 : 0,
	}
}
